package com.wclass.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.common.GeTuiUtil;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseAppUser;
import com.wclass.entity.BaseFocus;
import com.wclass.entity.CourseInfo;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.service.IBaseAppUserService;
import com.wclass.service.IBaseFocusService;
import com.wclass.service.ICourseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程基本信息 后台控制器
 * </p>
 *
 * @author Bruce
 * @since 2018-01-08
 */
@RestController
@RequestMapping("/sysCourseInfo")
public class SysCourseInfoController {

    @Autowired
    private ICourseInfoService iCourseInfoService;
    @Autowired
    private IBaseAppUserService iBaseAppUserService;
    @Autowired
    private IBaseFocusService iBaseFocusService;


    @ApiOperation("获取我的课程分页分页")
    @IgnoreAuth
    @RequestMapping(value = "/getCourses", method = RequestMethod.GET)
    public ReturnObject<List<CourseInfo>> getCourses( @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                      @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                      @ApiParam("审核状态") Integer auditState,
                                                      @ApiParam("课程名称") String name) {
        try {
            EntityWrapper<CourseInfo> ew = new EntityWrapper<>();
            if(auditState!=null){
                ew.eq("audit_state", auditState);
            }
            if(StringUtils.isNotBlank(name)){
                ew.like("name", name);
            }
            ew.orderBy("crt_time", false);
            Page<CourseInfo> obj = iCourseInfoService.selectPage(new Page<>(offset, limit), ew);
            return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(12, "系统偷懒了");
        }
    }

    @ApiOperation("对课程进行审核")
    @IgnoreAuth
    @RequestMapping(value = "/updateAuditState", method = RequestMethod.GET)
    public ReturnObject updateAuditState(@ApiParam("审核状态") @RequestParam String auditState,
                                         @ApiParam("审核意见") @RequestParam String auditOpinion,
                                         @ApiParam("课程id") @RequestParam String id) {
        try {
            CourseInfo courseInfo = iCourseInfoService.selectById(Long.valueOf(id));
            if (courseInfo != null) {
                courseInfo.setAuditState(Integer.valueOf(auditState));
                courseInfo.setAuditOpinion(auditOpinion);
                courseInfo.setAuditTime(new Date());
                iCourseInfoService.updateById(courseInfo);

                if (auditState.equals("1")) {
                    //更新老师课程数量
                    BaseAppUser baseAppUser = new BaseAppUser();
                    baseAppUser.setId(courseInfo.getUserId());
                    baseAppUser.setCourseNum(iCourseInfoService.selectCount(new EntityWrapper<CourseInfo>().eq("user_id", courseInfo.getUserId()).eq("state", 0).eq("audit_state", 1)));
                    iBaseAppUserService.updateById(baseAppUser);

                    //课程新增给粉丝发送通知
                    EntityWrapper<BaseFocus> focusEw = new EntityWrapper<>();
                    focusEw.eq("info.focus_user_id", courseInfo.getUserId()).orderBy("info.crt_time", false);
                    List<BaseAppUser> userList = iBaseFocusService.selectFansList(focusEw);
                    String cids = "";
                    String mobileType = "";
                    for (BaseAppUser user : userList) {
                        mobileType = user.getMobileType();
                        if (user.getMobileType() != null && mobileType.equals("android")) {
                            cids += user.getCId() + ",";
                        } else if (user.getMobileType() != null && mobileType.equals("ios")) {
                            cids += user.getDeviceToken() + ",";
                        }
                    }
                    cids.substring(0, cids.length() - 1);
                    String content = "你关注的" + baseAppUser.getNickName() + "教师发布了新的课程，请注意查看。";
                    if (cids.split(",").length == 1) {
                        GeTuiUtil.pushMessageToSingle(mobileType, cids, content);
                    } else if (cids.split(",").length > 1) {
                        GeTuiUtil.batchPushSingleMessage(cids, content);
                    }
                }

                return new ReturnObject(0, "审核成功");
            } else {
                return new ReturnObject(1, "审核失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }


}

